{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our lab computers do not allow user-installation of packages by default, so this notebook installs a package (demoing with the `yfinance` package for downloading Yahoo Finance data). The notebook then adds your user directory to the Python path so that you can import the package from the location it may be installed on our lab computers.\n",
    "\n",
    "You can run this notebook once to install a package on a lab computer. Then in the future, you should be able to import that package without reinstalling, but will need to run the part of this notebook that adds your user directory to the Python path.\n",
    "\n",
    "** Note the approach below is Windows specific.** If you have a Mac, you should (hopefully) be able to install packages yourself without this complicated approach using `conda` and/or `pip`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Package installation\n",
    "\n",
    "The next cell installs a package in your user directory.\n",
    "\n",
    "You should only need to do this once on a given lab computer (at least until the machine is reset, which happens periodically). Running more than once won't do any harm, but takes some extra time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install yfinance --user"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Package import\n",
    "\n",
    "Because the package is installed in your user directory, Python doesn't know how to find it by default (i.e., you can't import with a simple `import yfinance`). First, you need to add your user directory to the Python path so Python knows where to look for the package. The following cell does this, and you'd need to run it in any notebook where you want to import the package."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # Old approach:\n",
    "#\n",
    "# username = \"lcdstein\"\n",
    "#\n",
    "# import sys\n",
    "# sys.path.append('c:\\\\Users\\\\\" + username + \"\\\\AppData\\\\Roaming\\\\Python\\\\Python39\\\\site-packages')\n",
    "# for p in sys.path:\n",
    "#     print(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add user directory to sys.path so Python knows where to find user-installed packages\n",
    "\n",
    "import sys\n",
    "import os\n",
    "\n",
    "# Get the current user's name\n",
    "username = os.getenv(\"USERNAME\") or os.getenv(\"USER\")\n",
    "\n",
    "# Construct the user site-packages path\n",
    "python_version = f\"Python{sys.version_info.major}{sys.version_info.minor}\"\n",
    "site_packages_path = f\"C:\\\\Users\\\\{username}\\\\AppData\\\\Roaming\\\\{python_version}\\\\site-packages\"\n",
    "\n",
    "# Add it to sys.path if not already there\n",
    "if site_packages_path not in sys.path:\n",
    "    sys.path.append(site_packages_path)\n",
    "\n",
    "print(f\"Added {site_packages_path} to sys.path\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import yfinance"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "ad2bdc8ecc057115af97d19610ffacc2b4e99fae6737bb82f5d7fb13d2f2c186"
  },
  "kernelspec": {
   "display_name": "Python 3.9.12 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
